package com.jqk.shop_product.controller;

import com.alibaba.fastjson.JSON;
import com.jqk.shop_product.bean.Product;
import com.jqk.shop_product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:jiangqikun
 * @Date:2022/3/1 14:49
 **/

@RestController
@Slf4j
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/product/{pid}")
    public Product product(@PathVariable("pid") Integer pid) {
        Product product = productService.getById(pid);
        log.info("查询到商品:" + JSON.toJSONString(product));
        return product;
    }

    /**
     * 扣减库存
     */
    @GetMapping("/seata/reduce")
    public void reduce(Integer pid, int num) {
        Product product= productService.getById(pid);
        if (product.getStock() < num) {
            throw new RuntimeException("库存不足");
        }
        int i = 1 / 0;
        product.setStock(product.getStock() - num);
        productService.saveOrUpdate(product);
    }


}